'''
    Student user's methods
'''
import MySQLdb
import types
import os
from WeiboUser import WeiboUser
from conf import *
from MySqlDBHandle import MySqlDBHandle

class StudentUser(WeiboUser):
    
    def __init__(self, iUid):
        self.mUid = iUid
        WeiboUser.__init__(self, iUid, 'lu_student', 'student_follower_relationship') 
        
    def  process(self):
        self._WeiboUserAPIProcess()
        #if type(self.mJsonData) == types.DictType and not self.mJsonData.has_key('error'):
            #self._InsertStudentToDB()
        self._InsertFollowersToDB()

#    def _InsertStudentToDB(self):
#        if self._IsUserIdInDB():
#            return
#        else:
#            try:
#                lSQLStatement = "INSERT INTO LU_student (name, weiboId, LAST_UPDATE_TIMESTAMP, tutorID, gender) VALUES ('%s', %s, current_timestamp, 1, %s);"\
#                 % (self.mJsonData['screen_name'], self.mJsonData['id'], (self.mJsonData['gender'].find('f') == -1))
#                MySqlDBHandle.execSqlStatement(lSQLStatement)
#            except Exception, e:
#                #Add some process if necessary
#                print "Error in StudentUser.py InsertStudentToDB function with " + str(e)
                
#    def _IsUserIdInDB(self):
#        lSQLStatement = "SELECT * FROM LU_student WHERE weiboId = %s;" % self.mJsonData['id']
#        result = MySqlDBHandle.execSqlStatement(lSQLStatement)
#        if result is None:
#            return False
#        else:
#            return True
    

class Student():
    def process(self):
        lFilePathName = "logs\\currentStudentId.log"
        lSQLStatement = "SELECT WeiboID, id FROM lu_student %s ORDER BY id asc"
        lCurrentPostId = ""
        if os.path.isfile(lFilePathName):
            lFile_Object = open(lFilePathName, 'r')
            try:
                lCurrentPostId = lFile_Object.read()
            finally:
                lFile_Object.close()
        if lCurrentPostId == "":
            lSQLStatement = (lSQLStatement % "")
        else:
            lSQLStatement = (lSQLStatement % ("WHERE id > " + lCurrentPostId))
        try:
            result = MySqlDBHandle.execSqlStatement(lSQLStatement)
        except Exception, e:
            print "Error in StudentUser.py get Weibo_id function with error_message = " + str(e)     
        
        if type(result) == types.TupleType and len(result) > 0:
            for lIndexId in range(len(result)):
                lStudentId = result[lIndexId][0]
                lStudent=StudentUser(lStudentId);
                lStudent.process()
                #self._processSiglePost(lStudentId)
                lFile_object = open(lFilePathName, 'wb')
                try:
                    lFile_object.write(str(result[lIndexId][1]))
                finally:
                    lFile_object.close()
